package com.ds.infrastructure.audit.common.util;

import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 作用：
 *
 * @author WeiShaoying
 * @date 2020/7/28
 */
public class StringUtil {

    /**
     * 如果参数为空，返回空串
     *
     * @param obj
     * @return
     */
    public static String getString(Object obj) {
        return obj == null ? "" : obj.toString();
    }


    /**
     * @param obj
     * @param c
     * @return
     */
    public static String getString(Object obj, Class c) {
        if (obj == null) {
            return "";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS");
        if (c.equals(Date.class)) {
            Date date = (Date) obj;
            LocalDateTime localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            return localDate.format(formatter);
        } else if (c.equals(LocalDateTime.class)) {
            LocalDateTime localDate = (LocalDateTime) obj;
            return localDate.format(formatter);
        } else {
            return obj.toString();
        }
    }

    /**
     * 自动识别时间类型
     *
     * @param obj
     * @return
     */
    public static String getStringSupportTime(Object obj) {
        if (obj == null) {
            return "";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS");
        if (obj instanceof Date) {
            Date date = (Date) obj;
            LocalDateTime localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            return localDate.format(formatter);
        } else if (obj instanceof LocalDateTime) {
            LocalDateTime localDate = (LocalDateTime) obj;
            return localDate.format(formatter);
        } else {
            return obj.toString();
        }
    }


    /**
     * 删除末尾的符号
     *
     * @param sb
     * @return
     */
    public static String trimTail(StringBuilder sb) {
        if (sb == null || sb.length() <= 1) {
            return "";
        }
        String s = sb.toString();
        if (s.endsWith(",") || s.endsWith(" ") || s.endsWith(";") || s.endsWith("#")) {
            return sb.deleteCharAt(sb.length() - 1).toString();
        }
        return s;
    }

    /**
     * 去除字符串前后的中英文逗号
     *
     * @param str
     * @return
     */
    public static String trimComma(String str) {
        if (StringUtils.isEmpty(str)) {
            return str;
        }
        if (str.startsWith(",") || str.startsWith("，")) {
            str = str.substring(1);
        }
        if (str.endsWith(",") || str.endsWith("，")) {
            str = str.substring(0, str.length() - 1);
        }
        return str.trim();
    }

    public static boolean isNotEmpty(String str) {
        return str != null && str.length() >= 1 && str.trim().length() >= 1;
    }

    public static String trim(String str) {
        if (StringUtils.isEmpty(str) || str.trim().length() < 1 || "null".equals(str)) {
            return "";
        }
        return str.trim();
    }

    public static String trimObject(Object obj) {
        if (obj == null || StringUtils.isEmpty(obj.toString()) || "null".equals(obj.toString())) {
            return "";
        }
        return obj.toString().trim();
    }

    public static boolean isEmpty(String str) {
        return StringUtils.isEmpty(str) || str.trim().length() < 1 || "null".equals(str);
    }

    public static void main(String[] args) {
        Object date = new Date();

        String d1 = getString(date, Date.class);
        String d12 = getStringSupportTime(date);
        System.out.println("d12 = " + d12);

        Object localDateTime = LocalDateTime.now();
        String d2 = getString(localDateTime, LocalDateTime.class);
        String d22 = getStringSupportTime(localDateTime);
        System.out.println("d22 = " + d22);
    }
}
